package com.hctforgreen.greenservice.ui.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.model.AttrEntity;
import com.hctforgreen.greenservice.ui.widget.ExamineTypeListPopupView;

import java.util.List;

public class ExamineTypeAdapter extends BaseAdapter implements
		OnItemClickListener {
	private List<AttrEntity> mEntities;
	private Activity mActivity;
	private ListView mFirstLv;

	private ExamineTypeListPopupView mListPopupView;

	private int type;// 0： 资料大类别 1：资料小类别

	public ExamineTypeAdapter(ListView firstLv, Activity activity,
			List<AttrEntity> entities, AttrEntity currentFirstEntity,
			ExamineTypeListPopupView listPopupView, int type) {
		mActivity = activity;

		mEntities = entities;
		mFirstLv = firstLv;
		this.type = type;

		mListPopupView = listPopupView;
		mFirstLv.setAdapter(this);
		mFirstLv.setOnItemClickListener(this);
		initSelection(currentFirstEntity);
	}

	private void initSelection(AttrEntity currentFirstEntity) {
		if (currentFirstEntity == null) {
			return;
		}
		for (int i = 0; i < mEntities.size(); i++) {
			AttrEntity entity = mEntities.get(i);
			if (entity.id.equals(currentFirstEntity.id)) {
				mFirstLv.setSelection(i);
				entity.isSelected = true;
			}
		}

	}

	@Override
	public int getCount() {
		return mEntities.size();
	}

	@Override
	public Object getItem(int position) {
		return mEntities.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewCache viewCache;
		if (convertView == null) {
			convertView = View.inflate(mActivity,
					R.layout.item_series_books_first_lst, null);
			viewCache = new ViewCache(convertView);
			convertView.setTag(viewCache);

		} else {
			viewCache = (ViewCache) convertView.getTag();
		}

		final AttrEntity entity = (AttrEntity) getItem(position);

		viewCache.getItemNameTv().setText(entity.name);
		if (entity.isSelected) {
			viewCache.getSelectorLyt().setBackgroundResource(
					R.drawable.ic_first_popup_item_selected_bg);
		} else {
			viewCache.getSelectorLyt().setBackgroundDrawable(null);
		}
		return convertView;
	}

	class ViewCache {
		private View baseView;

		private LinearLayout selectorLyt;
		private TextView itemNameTv;

		public ViewCache(View view) {
			baseView = view;
		}

		public LinearLayout getSelectorLyt() {
			if (selectorLyt == null) {
				selectorLyt = (LinearLayout) baseView
						.findViewById(R.id.lyt_selector);
			}
			return selectorLyt;
		}

		public TextView getItemNameTv() {
			if (itemNameTv == null) {
				itemNameTv = (TextView) baseView
						.findViewById(R.id.tv_item_name);
			}
			return itemNameTv;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		final AttrEntity currentEntity = (AttrEntity) getItem(position);

		clearmEntitiesSelected(currentEntity);
		currentEntity.isSelected = true;

		/**
		 * 通知getView()刷新
		 */
		notifyDataSetChanged();
		renderDataLst(currentEntity);
	}

	private void clearmEntitiesSelected(final AttrEntity currentEntity) {
		for (AttrEntity entity : mEntities) {
			if (entity.id != currentEntity.id && entity.isSelected) {
				entity.isSelected = false;
			}
		}
	}

	private void renderDataLst(AttrEntity currentFirstEntity) {

		if (currentFirstEntity != null
		/* && currentFirstEntity.childList != null */) {
			mListPopupView.updateWindow(currentFirstEntity);
			mListPopupView.dismiss();
		}

	}

}
